package tedu.cn.submarine;

import javax.swing.ImageIcon;
import java.awt.Graphics;
import java.util.Random;

public abstract class SeaObject {
    public static final int LIVE = 0;
    public static final int DEAD = 1;
    private int state = LIVE;

    private int width;
    private int height;
    private int x;
    private int y;
    private int speed;

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public SeaObject(int width,int height,int x,int y){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
    }

    //潜艇
    public SeaObject(int width,int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT-height-110+1)+110;
        speed = rand.nextInt(3)+1;
    }

    //战舰、水雷、炸弹
    public SeaObject(int width,int height,int x,int y,int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }

    public abstract ImageIcon getImage();

    public boolean isLive(){
        return state==LIVE;
    }

    public boolean isDead(){
        return state==DEAD;
    }

    public void paintImage(Graphics g){
        if(isLive()){
            this.getImage().paintIcon(null,g,x,y);
        }
    }

    public abstract void move();

    public boolean isOutOfBounds(){
        return getX() >= World.WIDTH;
    }

    public boolean isHit(SeaObject other){
        int x1 = this.getX() - other.getWidth();
        int x2 = this.getX() + this.getWidth();
        int y1 = this.getY() - other.getHeight();
        int y2 = this.getY() + this.getHeight();
        int x = other.getX();
        int y = other.getY();
        return x >= x1 && x <= x2
                &&
                y >= y1 && y <= y2;
    }

    public void goDead(){
        state = DEAD;
    }
}
